﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using RockCompany.Common.Forms;
using RockCompany.Common.Lib;
using RockCompany.Master.Model;
using RockCompany.Master.Constant;
using RockCompany.Master.DTO;
using RockCompany.Master.Controller;
using RockCompany.Common.Constant;

namespace RockCompany.Master.View
{
    public partial class SW01B : ParentFrm
    {
        private Translator trans;
        private string sUserName; // login user
        private UserAuthenticate uAuth;
        private bool isChooser;

        // Controller
        private SW01FormCtrl ctrlSW01 = new SW01FormCtrl();

        // Model
        private SW01FormModel modelSW01 = new SW01FormModel();

        public SW01B()
        {
            InitializeComponent();
        }

        public SW01B(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            InitializeComponent();

            this.trans = translator;
            trans.controlTranslate(this);
            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUName);
            sUserName = sUName;
            this.dgvWOList.AutoGenerateColumns = false;
            modelSW01.translator = trans;
            modelSW01.buildDataGrid(dgvMaterialList, CreateMaterialProperties(), new string[] { NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU }.ToList<string>());
        }

        public List<PropertyInfo> CreateMaterialProperties()
        {
            NGUYEN_VLIEU_VTU_DTO nvlDTO = new NGUYEN_VLIEU_VTU_DTO();
            CONG_LENH_SX_CT clctDTO = new CONG_LENH_SX_CT();

            List<PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            
            properties.Add(nvlDTO.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_MA_NVL_VTU));
            properties.Add(clctDTO.GetType().GetProperty(CONG_LENH_SX_CT.N_TY_LE));
            properties.Add(clctDTO.GetType().GetProperty(CONG_LENH_SX_CT.N_TRONG_LUONG));
            properties.Add(clctDTO.GetType().GetProperty(CONG_LENH_SX_CT.N_SO_LAN));
            properties.Add(clctDTO.GetType().GetProperty(CONG_LENH_SX_CT.N_TONG_LUONG_SX));

            properties.Add(nvlDTO.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_DON_GIA));
            properties.Add(nvlDTO.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_TY_GIA));
            properties.Add(clctDTO.GetType().GetProperty(CONG_LENH_SX_CT.N_EX_RATE));
            properties.Add(clctDTO.GetType().GetProperty(CONG_LENH_SX_CT.N_GHI_CHU));

            return properties;
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            DataTable res = ctrlSW01.SearchWO(CreateWODTO(SW01AConst.F_SEARCH_CD));
            CommonModel.bindDGV(dgvWOList, res);
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            this.Hide();
            SW01A frm_SW01A = new SW01A(trans, sUserName, this);
            frm_SW01A.Show();
            btSearch_Click(sender, e);
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // Create Work Order DTO
        public WorkOrderDTO CreateWODTO(int funcCd)
        {
            WorkOrderDTO woDTO = new WorkOrderDTO();
            if (funcCd == SW01AConst.F_SEARCH_CD)
            {
                woDTO.ProductNo = tbProductNoSrc.Text.Trim();
                woDTO.LotNo = tbLotNoSrc.Text.Trim();
            }

            return woDTO;
        }

        private void dgvWOList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > 0)
            {
                WorkOrderDTO woDTO = new WorkOrderDTO();
                woDTO.LotNo = dgvWOList[colWOLotNo.Name,e.RowIndex].Value.ToString();
                woDTO.WOType = SW01AConst.NEW_TYPE;
                if (SW01AConst.CB_MODIFICATION_TYPE.Equals(dgvWOList[colWOType.Name, e.RowIndex].Value.ToString()))
                {
                    woDTO.WOType = SW01AConst.MODIFICATION_TYPE;
                }
                
                dgvMaterialList.DataSource = null;
                CommonModel.bindDGV(dgvMaterialList, modelSW01.SearchWODetails(woDTO));
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            List<string> checkedList = CommonModel.getCheckedList(colChk.Name, colWOLotNo.Name, dgvWOList);

            if (checkedList.Count > 0)
            {
                int removedCount = modelSW01.DeleteWO(checkedList);
                btSearch_Click(sender, e);
                if (checkedList.Count == removedCount)
                {
                    InforDialog.showMessageBox(trans.getMessageCaption(CommonConstantIF.InforBoxCap),
                                                  trans.getMessageCaption(CommonConstantIF.OPERATION_SUCCESS));
                }
                else
                {
                    InforDialog.showMessageBox(trans.getMessageCaption(CommonConstantIF.InforBoxCap),
                                                  trans.getMessageCaption(CommonConstantIF.OPERATION_FAILURE));
                }
            }
            else
            {
                InforDialog.showMessageBox(trans.getMessageCaption(CommonConstantIF.InforBoxCap),
                                                trans.getMessageCaption(CommonConstantIF.SELECTED_ROW));
            }
        }

        // Interface with SI02 - Phieu lanh NVL
        public void callChooser(bool b)
        {
            isChooser = b;
            btSelect.Enabled = b;
            btAdd.Enabled = !b;
            btDelete.Enabled = !b;
            btModify.Enabled = !b;
        }

        public List<string> getSelectedWO()
        {
            return CommonModel.getCheckedList(colChk.Name, colWOLotNo.Name, dgvWOList);
        }

        private void btModify_Click(object sender, EventArgs e)
        {
            List<string> lst = CommonModel.getCheckedList(colChk.Name, colWOLotNo.Name, dgvWOList);
            if (lst.Count > 0)// && modelSW01.IsNewWO(lst.ElementAt(0)))
            {
                SW01A frm_SW01A = new SW01A(trans, sUserName, this);
                frm_SW01A.isNew = false;
                frm_SW01A.choosenLotNo = lst.ElementAt(0);
                this.Hide();
                frm_SW01A.Show();
                btSearch_Click(sender, e);
            }
            else
            {
                InforDialog.showMessageBox(trans.getMessageCaption(CommonConstantIF.InforBoxCap),
                                           trans.getMessageCaption(CommonConstantIF.SELECTED_ROW));
            }
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            if (dgvWOList.CurrentRow != null)
            {
                int index = dgvWOList.CurrentRow.Index;
                string lotNo = dgvWOList.Rows[index].Cells[colWOLotNo.Name].Value.ToString();
                if (modelSW01.export2Excel(InforDialog.showSaveFileDlg(this), lotNo))
                {
                    InforDialog.showInforBox(this, trans.getMessageCaption(CommonConstantIF.SaveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                }
                else
                    InforDialog.showErrorBox(this, trans.getMessageCaption(CommonConstantIF.SaveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }
    }
}
